// import { Message } from 'element-ui'
import { getModule, saveContentCanvas } from '@/api/dag'
import { cloneDeep } from "lodash";

const dag = {
  state: {
    moduleLis: [],
    dagStructure: {},
  },
  actions: {
    getModuleLis: ({ commit }) => {
      commit('GETMODULELIS')
    },
    saveCanvas: ({ commit }, parameter) => {
      commit('SAVECANVAS', parameter)
    },
    handleStructure: ({ commit }, parameter) => {
      console.log("handle param")
      commit('HANDLESTRUCTURE', parameter)
    }
  },
  mutations: {
    GETMODULELIS: async (state) => {
      let res = await getModule()
      state.moduleLis = res.data
    },
    SAVECANVAS: async (state, parameter) => {
      // console.log(state)
      let { allData, thisData, canvas_id, fn } = parameter
      canvas_id = canvas_id - 0
      let edge = allData.cells.filter(e => e.shape == 'edge')
      let canvas_components = []
      allData.cells.forEach(e => {
        let tempIn = []
        let tempOut = []
        if (!e.data) return
        let params = e.data.versions[0].param_list
        params.forEach(e => {
          e.module_param_id = e.validation.module_param_id
          e.component_id = thisData.id
          e.key = e.type
          // console.log(e.defaultValue, "e.defaultValue");
          e.value = e.defaultValue
          if ( !e.disableCondition || e.disableCondition.type == 0 ) {
            e.disabled = false
          }
          if ( e.disableCondition && e.disableCondition.type == 1 ) {
            e.disabled = true
          }
          if ( e.disableCondition && e.disableCondition.type == 2 && e.disableCondition.value ) {
            let targetParam = params.filter(ele => ele.name == e.disableCondition.targetParam)
            e.disabled = new RegExp(e.disableCondition.targetParam).test(targetParam[0].defaultValue)
          }
        })
        e.data.ports.in.forEach(ele => {
          let thisEdge = edge.filter(item => item.target.port == ele.id)
          let thisCell = thisEdge[0] && thisEdge[0].source.cell
          let sourceCell = allData.cells.filter(e => e.id == thisCell)
          let sourceOut = sourceCell[0] && sourceCell[0].data.ports.out
          let target_port = 0
          sourceOut && sourceOut.forEach((e, i) => {
            if (e.id == (thisEdge[0] && thisEdge[0].source.port)) {
              target_port = i
            }
          })
          if (!thisEdge[0]) return 
          tempIn.push({
            from_id: thisEdge[0] && thisEdge[0].source.cell,
            module_port_id: thisEdge[0] && thisEdge[0].target.port,
            target_port
          })
        })
        e.data.ports.out.forEach(ele => {
          let thisEdge = edge.filter(item => item.source.port == ele.id)
          let thisCell = thisEdge[0] && thisEdge[0].target.cell
          let targetCell = allData.cells.filter(e => e.id == thisCell)
          let targetIns = targetCell[0] && targetCell[0].data.ports.in
          let target_port = 0
          targetIns && targetIns.forEach((e, i) => {
            if (e.id == (thisEdge[0] && thisEdge[0].target.port)) {
              target_port = i
            }
          })
          if (!thisEdge[0]) return 
          tempOut.push({
            to_id: thisEdge[0] && thisEdge[0].target.cell,
            module_port_id: thisEdge[0] && thisEdge[0].source.port,
            target_port,
          })
        })
        let temp = {
          id: e.id,
          module_id: e.data.id,
          name: e.data.name,
          in: tempIn,
          out: tempOut,
          params,
          canvas_id
        }
        canvas_components.push(temp)
      })
      let canvasAllData = JSON.parse(sessionStorage.getItem('canvasAllData'))
      await saveContentCanvas({
        canvas_cells: {
          cells: JSON.stringify(allData.cells),
          canvas_id,
          id: canvasAllData && canvasAllData.data && canvasAllData.data.canvas_cells && canvasAllData.data.canvas_cells.id
        },
        canvas_components,
        id: canvas_id
      })
      if (fn) {
        fn()
      }
      this.$message({
        type: "success",
        message: "保存成功",
        offset: 60,
      });
    },
    HANDLESTRUCTURE: (state, parameter) => {
      console.log(parameter.thisData)
      console.log(parameter.thisData.id)
      state.dagStructure[parameter.thisData.id] = cloneDeep(parameter.thisData)
      console.log(state.dagStructure)
    }
  }
};

export default dag;
